var map = new BMap.Map("map");
var markers = [];
map.centerAndZoom("邯郸", 15);
//允许滚轮缩放
map.enableScrollWheelZoom(true);

// 显示邯郸市范围
function getBoundary() {
    var bdary = new BMap.Boundary();
    bdary.get("邯郸市", function (rs) {       //获取行政区域
        var count = rs.boundaries.length; //行政区域的点有多少个
        if (count === 0) {
            alert('未能获取当前输入行政区域');
            return;
        }
        var pointArray = [];
        for (var i = 0; i < count; i++) {
            var ply = new BMap.Polyline(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
            map.addOverlay(ply);  //添加覆盖物
            pointArray = pointArray.concat(ply.getPath());
        }
        map.setViewport(pointArray);    //调整视野
    });
}

getBoundary();

addMarker("环保护局监测站", 114.512853, 36.617593);
addMarker("东污水处理厂监测站", 114.544153, 36.618284);
addMarker("矿院监测站", 114.509015, 36.576479);
addMarker("丛台公园监测站", 114.496787, 36.622443);

function addMarker(text, lng, lat) {
    var point = new BMap.Point(lng, lat);
    var marker = new BMap.Marker(point);  // 创建标注
    map.addOverlay(marker);              // 将标注添加到地图中
    var label = new BMap.Label(text, {offset: new BMap.Size(20, -10)});
    marker.setLabel(label);
    markers.push(marker);
}

var myChart = echarts.init(document.getElementById("chart"));
myChart.showLoading();

$.get("/pm", function (response, status) {
    $("h4").text(response.level);
    for (var i = 0; i < response.caption.length; i++) {
        $(".caption").eq(i).text(response.caption[i]);
        $(".value").eq(i).text(response.data[i]);
    }
    var item_length = response.tableTitle.length;
    for (var j = 0; j < markers.length; j++) {
        var content = "";
        for (var i = 0; i < item_length; i++) {
            content = content + response.tableTitle[i] + ":" + response.td[j * item_length + i] + "<br/>"
        }
        var opts = {
            width: 200,     // 信息窗口宽度
            height: 100,     // 信息窗口高度
            title: response.td[j] // 信息窗口标题
        };
        var infoWindow = new BMap.InfoWindow(content);  // 创建信息窗口对象
        markers[j].addEventListener("click", function (e) {
            var p = e.target;
            var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
            map.openInfoWindow(infoWindow, point); //开启信息窗口
        });
        $(".time").text(response.time[0]);
        myChart.hideLoading();
        var option = {
            title: {
                text: "最近24小时空气质量指数(AQI)趋势",
                textAlign: "left"
            },
            xAxis: {
                type: 'category',
                data: response.table_item
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: response.table_data,
                type: 'line'
            }],
            grid: {
                left: "5%",
                right: "1%",
                top: 30,
                bottom: 30
            },
            backgroundColor: 'rgba(128, 128, 128, 0.8)',
            tooltip: {
                trigger: 'axis'
            }
        };
        myChart.setOption(option);
    }

}, "json");
